﻿/*舒彩光
*2010-05-08
*文件上传控件
*功能：上传文件并返回路径到文本框
*除三个基础js外，还需要Action.js提供支持。
*/
Function.registerNameSpace("Page");
//textBox为返回路径的文框，activeButton为激活上传框的按钮。activeButton可以不指定，单击文本框激活上传框
Page.Upload = function(textBox, activeButton) {
    var openType = 1;
    Function.registerClass(this);
    if (typeof (textBox) == "string") textBox = $$(textBox);
    if (!activeButton) {
        activeButton = textBox;
        openType = 2;
    }
    else if (typeof (activeButton) == "string") activeButton = $$(activeButton);
    this._activeButton = activeButton;
    this._textBox = textBox;

    this._fileFirst = false;
    this._uploadMode = false;
    
    this._openHandler = EventOperate.createDelegate(this, this._uploadOpen);
    this._preCloseHandler = EventOperate.createDelegate(this, this._uploadPreClose);
    this._formClickHandler = EventOperate.createDelegate(this, this._formClick);
    this._formSubmitHandler = EventOperate.createDelegate(this, this._formSubmit);
    this._fileFocusHandler = EventOperate.createDelegate(this, function(sender, e) { this._fileFirst = true; });
    this._fileBlurHandler = EventOperate.createDelegate(this, function(sender, e) { this._fileFirst = false; try{this._textBox.focus();}catch(err){} });
    this._clickCloseHandler = EventOperate.createDelegate(this, this._uploadClose);

    this._closeHandler = Function.createDelegate(this, this._uploadClose);
    this._closeTimeleg = new Function.Timelag(this._closeHandler, 400);
    this._closeLongTimeleg = new Function.Timelag(this._closeHandler, 1200);
    this._toDefaultModeHandler = Function.createDelegate(this, this._toDefaultMode);
    this._toDefaultModeTimeleg = new Function.Timelag(this._toDefaultModeHandler, 600);
    
    if (openType == 1) {
        EventOperate.addEvent(this._activeButton, "click", this._openHandler);
    } else {
        EventOperate.addEvent(this._activeButton, "focus", this._openHandler);
    }
    EventOperate.addEvent(this._textBox, "blur", this._preCloseHandler);
    this._create();
}

Page.Upload.prototype._create = function () {
    var oThis = this;
    this._uploadBox = document.addElement(document.body, "form");
    this._uploadBox.encoding = "multipart/form-data";
    this._uploadBox.method = "POST";
    this._uploadBox.target = Page.Upload.__uploadiframename+"_"+Math.random()*1000000;
    this._uploadBox.style.zIndex = 99999;

    this._uploadCBox = document.addElement(this._uploadBox, "div");
    this._uploadField = document.addElementAtEnd(this._uploadCBox, "input", Page.Upload.__uploadiframename + "_input", "file");
    this._uploadButton = document.addElementAtEnd(this._uploadCBox, "input", Page.Upload.__uploadiframename + "_submitButton", "submit");
    this._uploadButton.style.fontSize = "12px";
    this._uploadButton.value = "开始上传";

    this._uploadState = document.addElement(this._uploadBox, "div");
    this._uploadStateConent = document.addElement(this._uploadState, "div");

    this._uploadIframe = document.addElement(this._uploadBox, "iframe", this._uploadBox.target);
    this._uploadIframe.style.display = "none";
    this._uploadIframe.onload = function () { oThis._uploaded(); }
    this._uploadIframe.onreadystatechange = function () { if (document.IsIE && this.readyState == 'complete') oThis._uploaded(); else oThis._uploaded(); }
    this._uploadBox.className = "AGuang___Page___Upload";
    this._uploadState.className = "uploadState";

    this._closeButton = document.addElement(this._uploadBox, "div");
    this._closeButton.className = "closeButton";

    EventOperate.addEvent(this._uploadBox, "click", this._formClickHandler);
    EventOperate.addEvent(this._uploadBox, "submit", this._formSubmitHandler);
    EventOperate.addEvent(this._uploadField, "focus", this._fileFocusHandler);
    EventOperate.addEvent(this._uploadField, "blur", this._fileBlurHandler);
    EventOperate.addEvent(this._closeButton, "click", this._clickCloseHandler);
    this._sideAction = new Page.Action(this._uploadBox);
}

//设置上传处理文件，该文件要求上传完成后输出一个路径字符串
Page.Upload.prototype.SetAction = function(url) {
    this._uploadBox.action = url;
}

//文件的扩展名数组
Page.Upload.prototype.SetFileTypes = function(types) {
    this._fileTypes = types;
}

Page.Upload.prototype.Add_UploadedHanld = function (func) {
    EventOperate.addHandler(this, "Uploaded", func);
}

Page.Upload.prototype._uploaded = function() {
    if (this._uploadMode == false) return;
    this._textBox.value = this._uploadIframe.contentWindow.document.body.innerHTML.trim();
    var _h = EventOperate.getHandler(this, "Uploaded");
    if (_h) { _h(this, this._textBox.value); }
    this._uploadStateConent.className = "uploadState_ok";
    this._uploadStateConent.innerHTML = "文件上传完成";
    this._uploadMode = false;
    this._closeLongTimeleg.run();
}

Page.Upload.prototype._toDefaultMode = function() {
    document.setStyle(this._uploadCBox, { display: "block" });
    document.setStyle(this._uploadState, { display: "none" });
}

Page.Upload.prototype._uploadOpen = function(sender, e) {
    this._toDefaultMode();
    this._uploadBox.style.display = "block";
    this._uploadField.value = "";
    if (document.IsIE) {
        //this._uploadField.focus();
        //document.execCommand("selectall");
        //document.execCommand("Delete");
    }
    this._sideAction.AutoSide(this._activeButton);
    this._closeTimeleg.cancel();
    try{this._textBox.focus();}catch(err){}
}
Page.Upload.prototype._uploadPreClose = function (sender, e) {
    if (!this._uploadMode) this._closeTimeleg.run();
    return false;
}
Page.Upload.prototype._formClick = function(sender, e) {
    this._closeTimeleg.cancel();
    if(!this._fileFirst){
        try{this._textBox.focus();}catch(err){}
    }
}
Page.Upload.prototype._uploadClose = function(sender, e) {
    this._uploadBox.style.display = "none";
}
Page.Upload.prototype._formSubmit = function(sender, e) {
    var w = this._uploadCBox.offsetWidth;
    var h = this._uploadCBox.offsetHeight;
    document.setStyle(this._uploadCBox, { display: "none" });
    document.setStyle(this._uploadState, { width: w + 'px', height: h + 'px', display: "block" });
    if (this._uploadField.value == "") {
        this._uploadStateConent.className = "uploadState_error";
        this._uploadStateConent.innerHTML = "请选择要上传的文件!";
        this._toDefaultModeTimeleg.run();
        return false;
    }
    if (this._fileTypes) {
        var s = this._uploadField.value.lastIndexOf(".");
        var ext = this._uploadField.value.substr(s + 1).toLowerCase();
        var o = false;
        if (s > 0) {
            for (var i = 0, l = this._fileTypes.length; i < l; i++) {
                if (ext == this._fileTypes[i].toLowerCase()) {
                    o = true;
                    break;
                }
            }
        }
        if (!o) {
            this._uploadStateConent.className = "uploadState_error";
            this._uploadStateConent.innerHTML = "上传的文件格式不正确!";
            this._toDefaultModeTimeleg.run();
            return false;
        }
    }
    this._uploadStateConent.className = "uploadState_loading";
    this._uploadStateConent.innerHTML = "文件上传中，请稍候...";
    this._uploadMode = true;
    //this._uploadBox.submit();
    return true;
}
Page.Upload.__uploadiframename = "____AGuang_Page_Upload_iframe";